use std::time::Duration;

use bevy::{
    diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
    prelude::*,
};
use bevy_ui_builder::{
    animate::{Animator, AnimatorPlugin, LoopStrategy, Repeat},
    class::{GameCommands, Qb},
    widget::{root, sprite},
};
use interpolation::EaseFunction;

fn main() {
    App::new()
        .add_plugins((
            DefaultPlugins,
            AnimatorPlugin,
            FrameTimeDiagnosticsPlugin,
            LogDiagnosticsPlugin::default(),
        ))
        .add_systems(Startup, setup)
        .run();
}

fn setup(mut commands: Commands) {
    commands.spawn(Camera2dBundle::default());
    let mut gc = GameCommands::new(&mut commands);
    root((), &mut gc, |gc| {
        sprite(
            (
                test_example,
                test_class_sp1,
                test_class0,
                test_class1,
                test_class2,
                test_class3,
            ),
            gc,
        );
        sprite((test_class_sp2, test_class0, test_class2), gc);
        sprite((test_class_sp3, test_class1, test_class3), gc);
    });
}

fn test_example(_: Qb<SpriteBundle>, _: Qb<Animator>) {}

fn test_class_sp1(mut sprite: Qb<SpriteBundle>) {
    sprite.sprite.custom_size = Some(Vec2::new(100.0, 100.0));
    sprite.sprite.color = Color::RED;
}

fn test_class_sp2(mut sprite: Qb<SpriteBundle>) {
    sprite.sprite.custom_size = Some(Vec2::new(50.0, 50.0));
    sprite.sprite.color = Color::BLUE;
}

fn test_class_sp3(mut sprite: Qb<SpriteBundle>) {
    sprite.sprite.custom_size = Some(Vec2::new(50.0, 50.0));
    sprite.sprite.color = Color::GREEN;
    sprite.transform.translation = Vec3::new(0., 0., 1.);
}

fn test_class0(mut animator: Qb<Animator>) {
    animator
        .set_loop_strategy(LoopStrategy::Loop)
        .add_change()
        .set_retain_change(true)
        .set_duration(Duration::from_secs(1))
        .set_repeat(Repeat::Finite(1))
        .set_ease(EaseFunction::CubicOut.into())
        .set_transform_translation(Vec3::new(100., 0., 0.));
}

fn test_class1(mut animator: Qb<Animator>) {
    animator
        .set_loop_strategy(LoopStrategy::Loop)
        .add_change()
        .set_retain_change(true)
        .set_duration(Duration::from_secs(1))
        .set_repeat(Repeat::Finite(1))
        .set_ease(EaseFunction::CubicOut.into())
        .set_transform_translation(Vec3::new(0., -100., 0.));
}

fn test_class2(mut animator: Qb<Animator>) {
    animator
        .add_change()
        .set_retain_change(true)
        .set_duration(Duration::from_secs(1))
        .set_repeat(Repeat::Finite(1))
        .set_ease(EaseFunction::CubicIn.into())
        .set_transform_translation(Vec3::new(-100., 0., 0.));
}

fn test_class3(mut animator: Qb<Animator>) {
    animator
        .add_change()
        .set_retain_change(true)
        .set_duration(Duration::from_secs(1))
        .set_repeat(Repeat::Finite(1))
        .set_ease(EaseFunction::CubicOut.into())
        .set_transform_translation(Vec3::new(0., 100., 0.));
}
